﻿using Entity;
using Entity.Enum;
using Entity.ResultModel.RoomInfo;
using Entity.Room;
using IHotelBLL.IRoomBLL;
using IHotelDAL.IRoomDAL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBLL.RoomBLL
{
    public class RoomInfoBLL : IRoomInfoBLL
    {
        IRoomInfoDAL _roomInfoDAL;
        IRoomTypeDAL _roomTypeDAL;
        public RoomInfoBLL(IRoomInfoDAL roomInfoDAL, IRoomTypeDAL roomTypeDAL)
        {
            _roomInfoDAL = roomInfoDAL;
            _roomTypeDAL = roomTypeDAL;
        }

        //客房信息页面展示接口BLL
        public List<GetRoomInfo> GetRoomInfos(RoomStatusEnum status, int page, int limit, string name, out int count)
        {
            //linq客房信息表与客房类型表左连接查询
            var queryDatas = from u in _roomInfoDAL.GetEntities()
                             join d in _roomTypeDAL.GetEntities()
                             on u.TypeId equals d.Id into udtemp
                             from dd in udtemp.DefaultIfEmpty()
                             select new
                             {
                                 u.Id,
                                 u.Name,
                                 u.Status,
                                 u.Price,
                                 u.ImageName,

                                 u.AddDate,
                                 dd.TypeName
                             };

            //条件筛选
            if (!string.IsNullOrEmpty(name))
            {
                queryDatas = queryDatas.Where(u => u.Name.Contains(name));
            }

            var dbRoomInfos = queryDatas.Where(u => u.Name != null);

            //获取数据库中所有用户的数量
            count = dbRoomInfos.Count();

            //分页
            var userInfos = dbRoomInfos.OrderByDescending(u => u.AddDate).Skip((page - 1) * limit).Take(limit).ToList();

            //select
            var tempList = userInfos.Select(u =>
            {
                return new GetRoomInfo
                {
                    Id = u.Id,
                    Name = u.Name,
                    Price = u.Price,
                    Status = u.Status.ToString(),
                    ImageName = u.ImageName,
                    TypeName = u.TypeName
                };
            }).ToList();

            return tempList;
        }
        public List<RoomType> GetRoomInfos()
        {
            return _roomTypeDAL.GetEntities().OrderBy(r=>r.sort).ToList();
        }
        //添加接口BLL
        public bool CreateRoomInfo(string name, string typeId, decimal price,string imageName, out string msg)
        {
            //通过类别名词去找类别信息
            RoomInfo roomInfo = _roomInfoDAL.GetEntities().FirstOrDefault(c => c.Name == name);
            if (roomInfo != null)
            {
                msg = "名称已存在";
                return false;
            }

            RoomInfo entity = new()
            {
                Id = Guid.NewGuid().ToString(),
                Name = name,
                Status =  RoomStatusEnum.可入住,
                TypeId = typeId,
                Price = price,
                ImageName = imageName,
                AddDate = DateTime.Now
            };
            bool isSuccess = _roomInfoDAL.CreateEntity(entity);
            //三目运算符
            msg = isSuccess ? "创建成功" : "创建失败";
            return isSuccess;
        }


        //根据id获取信息
        public RoomInfo GetRoomInfoById(string id)
        {
            return _roomInfoDAL.GetEntities().FirstOrDefault(u => u.Id == id);
        }

        //更新接口BLL
        public bool UpdateRoomInfo(string id, string name, string typeId, decimal price, RoomStatusEnum status, string imageName, out string msg)
        {

            RoomInfo roomInfo = GetRoomInfoById(id);
            if (roomInfo == null)
            {
                msg = "未找到客房ID";
                return false;
            }
            roomInfo.Name = name;
            roomInfo.TypeId = typeId;
            roomInfo.Status = status;
            roomInfo.Price = price;
            roomInfo.ImageName = imageName;

            bool isSuccess = _roomInfoDAL.UpdateEntity(roomInfo);
            if (isSuccess)
            {
                msg = "修改成功";
                return true;
            }
            else
            {
                msg = "修改失败";
                return true;
            }
        }

        //删除
        public bool DeleteRoomInfo(string id, out string msg)
        {
            bool isSuccess = _roomInfoDAL.DeleteEntity(id);
            msg = isSuccess ? "删除成功" : "删除失败";
            return isSuccess;
        }

        //客房图片接口
        public bool RoomImg(string fileName, Stream stream, out string fileInfoId, out string msg)
        {
            //throw new NotImplementedException();

            //获取时间戳
            var timeSpan = DateTime.Now - new DateTime(1970, 1, 1);
            //获取毫秒数
            var seconds = timeSpan.TotalSeconds;
            //取整
            var first = Math.Round(seconds);
            //获取文件拓展名  abc.aaa.txt
            string extension = fileName.Substring(fileName.LastIndexOf("."));
            //字符串拼接
            StringBuilder ssbb = new StringBuilder();
            ssbb.Append(first);
            ssbb.Append(extension);
            //创建新文件名
            string newFileName = ssbb.ToString();
            //获取数据库上下文
            var dbContext = _roomInfoDAL.GetContext();

            //获取当前项目运行的文件夹路径
            string path = Directory.GetCurrentDirectory();
            //清空之前的字符串
            ssbb.Clear();
            ssbb.Append(path);
            ssbb.Append(@"\wwwroot\img\");
            ssbb.Append(newFileName);
            string filePath = ssbb.ToString();


            //写一个文件到wwwroot img文件夹
            using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.CopyTo(fileStream);
            }

            msg = "上传成功";
            fileInfoId = "";
            return true;

        }

        public void BatchDeleteRoomInfo(List<string> ids)
        {
            //查询需要删除的类别
            List<RoomInfo> room = _roomInfoDAL.GetEntities().Where(u => ids.Contains(u.Id)).ToList();
            foreach (var rooms in room)
            {
                //真删除
                bool isSuccess = _roomInfoDAL.DeleteEntity(rooms);
            }
        }

        public List<GetRoomInfo> GetRoomInfos(string typeId)
        {
            if(typeId == null)
            {
                var datas = from r1 in _roomInfoDAL.GetEntities()
                            join r2 in _roomTypeDAL.GetEntities()
                            on r1.TypeId equals r2.Id
                            select new GetRoomInfo
                            {
                                Id = r1.Id,
                                TypeName = r2.TypeName,
                                AddDate = r1.AddDate.ToString("yyyy年MM月dd日"),
                                ImageName = r1.ImageName,
                                Name = r1.Name,
                                Price = r1.Price,
                                Status = r1.Status.ToString(),
                            };
                return datas.ToList();
            }
            return (from r1 in _roomTypeDAL.GetEntities().Where(r => r.Id == typeId)
                        join r2 in _roomInfoDAL.GetEntities()
                        on r1.Id equals r2.Id
                        select new GetRoomInfo{
                            Id = r2.Id,
                            TypeName = r1.TypeName,
                            AddDate = r2.AddDate.ToString("yyyy年MM月dd日"),
                            ImageName = r2.ImageName,
                            Name = r2.Name,
                            Price = r2.Price,
                            Status = r2.Status.ToString(),
                        }).ToList();
        }

        public List<SelectOptionModel> GetRoomNameOptions(string typeId)
        {
            return _roomInfoDAL.GetEntities().Select(c => new SelectOptionModel()
            {
                Id = c.Id,
                Name = c.Name
            }).ToList();
        }
        //房间号码下拉选
        public List<SelectOptionModel> GetRoomInfoOptions()
        {
            return _roomInfoDAL.GetEntities().Select(c => new SelectOptionModel()
            {
                Id = c.Id,
                Name = c.Name
            }).ToList();
        }
    }
}
